import json


class PaperInfo:
    def __init__(self, json_object):
        self.id = json_object.get("id")
        self.entities = json_object.get("entities")
        self.magId = json_object.get("magId")
        self.journalVolume = json_object.get("journalVolume").replace("\n", "").replace('\"', '\\"')
        self.journalPages = json_object.get("journalPages").replace("\n", "").replace('\"', '\\"')
        self.pmid = json_object.get("pmid")
        self.year = json_object.get("year")
        self.outCitations = json_object.get("outCitations")
        self.s2Url = json_object.get("s2Url")
        self.s2PdfUrl = json_object.get("s2PdfUrl")
        self.authors = json_object.get("authors")
        self.journalName = json_object.get("journalName").replace("\n", "").replace("\\", "\\\\").replace('\"', '\\"')
        # 注意，此处的\要在"前面替换，不然会出错
        self.paperAbstract = json_object.get("paperAbstract").replace("\n", "").replace("\\", "\\\\") \
            .replace('\"', '\\"').replace("\t", "").replace("\r", "")
        self.inCitations = json_object.get("inCitations")
        self.pdfUrls = json_object.get("pdfUrls")
        self.title = json_object.get("title").replace("\n", "").replace("\\", "\\\\").replace('\"', '\\"')
        self.doi = json_object.get("doi").replace("\n", "").replace("\\", "\\\\").replace('\"', '\\"')
        self.sources = json_object.get("sources")
        self.doiUrl = json_object.get("doiUrl")
        self.venue = json_object.get("venue").replace("\n", "").replace("\\", "\\\\").replace('\"', '\\"')
        self.rdf_list = []

    def get_rdf(self):
        self.rdf_list.append('<_:{}>\t<id>\t"{}"\t.'.format(self.id, self.id))
        for entity in self.entities:
            self.rdf_list.append('<_:{}>\t<entities>\t"{}"\t.'.format(self.id, entity))
        self.rdf_list.append('<_:{}>\t<magId>\t"{}"\t.'.format(self.id, self.magId))
        self.rdf_list.append('<_:{}>\t<journalVolume>\t"{}"\t.'.format(self.id, self.journalVolume))
        self.rdf_list.append('<_:{}>\t<journalPages>\t"{}"\t.'.format(self.id, self.journalPages.strip()))
        self.rdf_list.append('<_:{}>\t<pmid>\t"{}"\t.'.format(self.id, self.pmid))
        self.rdf_list.append('<_:{}>\t<year>\t"{}"\t.'.format(self.id, self.year))
        for pdfUrl in self.pdfUrls:
            self.rdf_list.append('<_:{}>\t<pdfUrls>\t"{}"\t.'.format(self.id, pdfUrl))
        self.rdf_list.append('<_:{}>\t<s2Url>\t"{}"\t.'.format(self.id, self.s2Url))
        self.rdf_list.append('<_:{}>\t<s2PdfUrl>\t"{}"\t.'.format(self.id, self.s2PdfUrl))
        self.rdf_list.append('<_:{}>\t<journalName>\t"{}"\t.'.format(self.id, self.journalName))
        self.rdf_list.append('<_:{}>\t<paperAbstract>\t"{}"\t.'.format(self.id, self.paperAbstract))
        self.rdf_list.append('<_:{}>\t<title>\t"{}"\t.'.format(self.id, self.title))
        self.rdf_list.append('<_:{}>\t<doi>\t"{}"\t.'.format(self.id, self.doi))
        for source in self.sources:
            self.rdf_list.append('<_:{}>\t<sources>\t"{}"\t.'.format(self.id, source))
        self.rdf_list.append('<_:{}>\t<doiUrl>\t"{}"\t.'.format(self.id, self.doiUrl))
        self.rdf_list.append('<_:{}>\t<venue>\t"{}"\t.'.format(self.id, self.venue))

        for author in self.authors:
            ids = author.get("ids")
            author_name = author.get("name").replace('\"', '').replace("\n", "").replace('\\', '')
            if len(ids) > 0:
                author_id = ids[0]
            else:
                # 会出现乱码的情况，所以把无id的作者名置为anonymity
                author_id = "anonymity"
            self.rdf_list.append('<_:{}>\t<name>\t"{}"\t.'.format(author_id, author_name))
            self.rdf_list.append('<_:{}>\t<{}>\t<_:{}>\t.'.format(self.id, "authoris", author_id))

        for outcitation in self.outCitations:
            self.rdf_list.append('<_:{}>\t<{}>\t<_:{}>\t.'.format(self.id, "outcitationis", outcitation))
        for incitation in self.inCitations:
            self.rdf_list.append('<_:{}>\t<{}>\t<_:{}>\t.'.format(self.id, "incitationis", incitation))
        return self.rdf_list


class Author:
    def __init__(self, name, author_id):
        self.name = name
        self.id = author_id


def reformat(json_line, f2):
    jsonObject = json.loads(json_line)
    paper = PaperInfo(jsonObject)
    rdf_list = paper.get_rdf()
    rdf_string = ""
    for rdf in rdf_list:
        rdf_string = rdf_string + rdf + "\n"
    f2.write(rdf_string)


def read_write_file(path_a, f_a):
    f2 = open(f_a, 'w', encoding='utf-8')
    i = 0
    with open(path_a, 'r', encoding='utf-8') as f1:
        for line in f1:
            i += 1
            # if i % 1000 == 0:
            #     print("执行到了第%d条" % i)
            reformat(line, f2)


# path = "s2-corpus-001"
# format_data = "s2rdfs/s2-corpus-001.rdf"
path = "D:/Testtext/dgraph/100.json"
format_data = "D:/Programfiles/dgraph-windows-amd64/100format.rdf"

if __name__ == '__main__':
    read_write_file(path, format_data)
